# -*- coding:utf-8
"""
作者: Jia
日期: 2022年04月17日
描述: https://leetcode-cn.com/problems/merge-two-sorted-lists/
"""
# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        head = ListNode()
        temp = head
        while list1 and list2:
            temp.next = ListNode()
            temp = temp.next
            if list1.val < list2.val:
                temp.val = list1.val
                list1 = list1.next
            else:
                temp.val = list2.val
                list2 = list2.next

        new_head = list1 if list1 else list2
        temp.next = new_head
        return head.next
